<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<base href="<%=basePath%>"/>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>生产计划列表</title>
		<link rel="stylesheet" href="res/layui/css/layui.css">
		<script src="res/js/jquery-2.1.4.min.js" type="text/javascript" ></script>
		<script src="res/layui/layui.js"></script>
	</head>
	<body>
	<div style="padding-top: 10px;margin-bottom: -10px;">
		计划人：
		<div class="layui-inline" style="width: 140px;">
			<input class="layui-input" name="staffName" id="staffName"
				autocomplete="off">
		</div>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		开始日期：
		<div class="layui-input-inline">
       		<input type="text" class="layui-input" id="designEmetime" placeholder="请选择一个日期" name="designEmetime">
      	</div>
		<button class="layui-btn" data-type="reload" onclick="reloadData()">搜索</button>
		<button data-type="auto" class="layui-btn layui-btn-normal"
			onclick="add_article_sort()">新增生产计划</button>
	</div>
	<table class="layui-table" id="prodesign" lay-filter="demo"></table> 
	  
	
	<script type="text/html" id="demo">
  		<a class="layui-btn layui-btn-xs" lay-event="edit">生产明细</a>
		<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="detail">查看审核进度</a>
  		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	</script> 
	
	<div id="addsort_style" style="display: none" class="layui-form-item">
		<div class="add_content" class="layui-form-item">
			<div style="margin: 10px;" class="layui-form">
				<div class="layui-inline">
      				<label class="layui-form-label">商品:</label>
					<div class="layui-input-inline">
						<select id="boards" style="width: 100px; height: 40px;" lay-verify="required" lay-search="">
								<option value="">请选择商品</option>
						</select>
					</div>
				</div>
				</select> &nbsp;&nbsp;&nbsp;&nbsp;数量:&nbsp;&nbsp;<input type="text"
					style="width: 80px; height: 35px;" id="wareCount" name="wareCount">
					
				<button class="layui-btn layui-btn-normal" id="addcure" name="">
					增加</button>
			</div>
			<div style="margin: 10px; width: 700px;">
				<form action="" method="post" class="layui-form" id="fdcureAdd">
				<div class="layui-inline">
						<label class="layui-form-label">日期范围</label>
						<div class="layui-input-inline">
     						<input class="layui-input" id="test6" name="test5" placeholder="请选择" type="text">
     					</div>
  					 </div>
					</div>
					</br>
					<!--消息汇总-->
					<table class="layui-table" id="curedan" style="margin-buttom: 9px;">
						<tr class="text-c">
							<th width="60">经办人</th>
							<th width="60">计划总量</th>
							<th width="100">计划总价</th>
						</tr>
						<tr class="text-c">
							<!-- 订单表的id -->
							<th width="60" style="background: #f3f3f3;">${staff.staffName}</th>
							<th width="60" style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
								<input type="text" id="sumNum" name="designCount"
								style="width: 60px; border-style: none; background: #f3f3f3;"readonly>
							</th>
							<th width="100" style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
								<input type="text" id="sumMoney" name="designMoney" style="width: 60px; border-style: none; background: #f3f3f3;"
								readonly>元
							</th>
	
						</tr>
					</table>
	
					<table class="layui-table" id="cureTab" style="margin-top: 15px;">
						<tr class="text-c">
							<th width="150">药品生产厂家</th>
							<th width="100">商品名称</th>
							<th width="60">商品单价/元</th>
							<th width="60">商品数量</th>
							<th width="60">商品总价/元</th>
							<th width="40">操作</th>
						</tr>
						<!-- 循环取出明细订单 -->
	
					</table>
					
					<table class="layui-table" id="cure" style="margin-top: 15px;">
						<tr class="text-c">
							<th width="100">配料名称</th>
							<th width="60">配料单价/元</th>
							<th width="60">配料数量</th>
							<th width="60">原库存</th>
							<th width="60">现库存</th>
							<th width="60">总价/元</th>
							<th width="100">状态</th>
						</tr>
					</table>
			</div>
		</div>
	</div>
	<script type="text/javascript">
	
	
		var table;
		var loadIndex;
		var openIndex;

		function showForm(designId) {
			var url;
			if (designId != null) {
				url = "designmx/mxshowList.action?designId="+designId;
			} 
			openIndex = layer.open({
				type : 2//样式
				,
				skin : 'layui-layer-molv'//样式
				,
				area : [ '800px', '350px' ],
				title : "计划生产明细"//标题
				,
				id : 'mesFrom' //防止重复弹出
				,
				content : url,
				shade : [ 0.8, '#393D49' ] //显示遮罩
				,
				shadeClose : true//点击也能遮罩层关闭
				,
				anim : 2
			//弹出动画 
			});
		}
		
		function deleteRec(designId, i) {
			var url = "dProdesign/deleteprode.action";
			$.post(url, {
				"designId" : designId
			}, function(info) {
				layer.msg(info.mes);
				layer.close(i);
			});
		}
		
		

		layui.use([ 'table' ], function() {

			table = layui.table;
			loadIndex = layer.load();
			//方法级渲染
			table.render({
				loading : true,
				id : 'designId',
				page : true,
				height : 383,
				size : "sm",
				elem : '#prodesign',
				url : 'dProdesign/showList.action',
				method : 'post',
				where : {

				},
				cols : [ [ {
					field : 'proName',
					title : '计划人',
					width : 95,
					align : 'center'
				}, {
					field : 'designNumber',
					title : '计划编号',
					width : 110,
					align : 'center'
				},{
					field : 'designMoney',
					title : '计划总金额',
					width : 110,
					align : 'center'

				}, {
					field : 'designCount',
					title : '商品总数量',
					width : 120,
					align : 'center'

				}, {
					field : 'tabstrtime',
					title : '计划开始时间',
					width : 110,
					align : 'center'
				},{
					field : 'tabstrtimes',
					title : '计划结束时间',
					width : 100,
					align : 'center'
				},{
					field : 'designIsvo',
					title : '审核状态',
					width : 120,
					align : 'center',
					templet:"#titleTpl"
				},{
					fixed : 'right',
					width : 230,
					align : 'center',
					toolbar : '#demo'
				} ] ]

				,
				done : function(res, curr, count) {
					layer.close(loadIndex);//加载层关闭  
				}
			});
			//监听工具条
			table.on('tool(demo)', function(obj) {
				var data = obj.data;
				if (obj.event === /*  */'del') {
					layer.confirm('确认删除？', function(index) {
						var i = layer.load();
						deleteRec(data.designId, i);
						obj.del();
						layer.close(index);
					});
				} else if (obj.event === 'edit') {
					showForm(data.designId);
				}else if(obj.event === 'detail'){
					if(data.designIsvo!=0){
						information(data.designId);//查询审核情况的方法
					}else{
						layer.msg('等待审核',{icon: 5,anim: 6});
					}
				}
			});

			$('.demoTable .layui-btn').on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});
		});
		
		function reloadData(){
			table.reload('designId', {
				where : {
					'staffName' : $("#staffName").val(),
					'designEmetime' : $("#designEmetime").val()
				}
			});
		}
		
		
		/**
		查看审核情况
	*/
	function information(designId){
		openIndex = layer.open({
			type : 2//样式
			,
			skin : 'layui-layer-molv'//样式
			,
			area : [ '50%', '70%' ],
			title : "审核记录"//标题 
			,
			id : 'queryExam' //防止重复弹出
			,
			content : 'pages/SH/queryExam.jsp?entryId='+designId,
			shade : [ 0.8, '#393D49' ] //显示遮罩 
			,
			shadeClose : true//点击也能遮罩层关闭
			,
			anim : 2
		//弹出动画 
		});
	}
		
		
		
		/**添加**/
		function add_article_sort(index) {
			var one = layer.open({
				type : 1,
				title : '添加生产计划',
				maxmin : true,
				shadeClose : false, //点击遮罩关闭层
				area : [ '720px', '90%' ],
				content : $('#addsort_style'),
				btn : [ '提交', '取消' ],
				yes : function(index, layero) {
					//获取明细table中的数据
					var str = ""; //需要封装的字符串
					var rows = $("#cureTab tr").length - 1;//表格的总行数
					//表格中所有的值.每个单元格用~隔开,每行使用_隔开.
					for (i = 0; i < rows; i++) {
						$("#cureTab tr:gt(0):eq(" + i + ") td").each(function() {
							str += $(this).text() + "~";
						});
						str += "_";
					}
					var url = "dProdesign/showAdd.action?tabstr=" + str;
					var data = $("#fdcureAdd").serialize();//序列号表单
					var state=true;
					$(".nowNum").each(function(){
						if($(this).text()<0){
							state=false;
						}
					})
					if(state){
						
						$.post(url, data, function(mes) {
							parent.layer.msg(mes.mes);
							layer.close(index);
							location.reload();//重新加载页面
						},"json");
					}else{
						layer.msg('配料库存不足',{icon: 5,anim: 6});  
					}
					

				}
		
			})
			
			
		}
		
		//加载商品下拉框
		function getBoards() {
			$
					.ajax({
						url : 'ware/showAll.action?page=1&limit=999',
						type : 'POST',
						success : function(data) {
							var boards = eval(data.data);
							$(boards).each(
								function(index) {
									var board = boards[index];
									var optionStr= "<option value='"+board.wareId+"'>"+board.wareName+"</option>";
								    $("#boards").append(optionStr);
									form.render();
							});
						},
						error : function(data) {
							alert('查找板块报错');
						}
					});
		}
		
		//加载商品下拉框
		layui.use('form', function() {
			form = layui.form;
			getBoards();
		});
		
		
		//加载日期格式
		layui.use('laydate', function() {
			var laydate = layui.laydate;
			//日期范围
			  laydate.render({
			    elem: '#test6'
			    ,min: 0//0天前
				,max: 100//一百天内
			    ,range: true
			  });
			
			  //前后若干天可选，这里以7天为例
			  //laydate.render({
			  //  elem: '#test-limit2'
			  //  ,min: -7
			  //  ,max: 7
			 // });
			
			
			  //常规用法
			  laydate.render({
			    elem: '#designEmetime'
			  });
			  
		});
		var state=0;
		$(function() {
			//同步
			$.ajaxSetup({
				async : false
			});
			//绑定增加按钮
			$("#addcure").click(
				function() {
					//获取明细table中的数据
					var str = ""; //需要封装的字符串
					var wName = document.getElementsByName("waName");
					for (i = 0; i < wName.length; i++) {
						str += wName[i].innerHTML;
					}
					//商品名称
					var wareName = $("#boards").val();
					//商品数量
					var wareCount = $("#wareCount").val();
					
  					if (wareName == null || wareName == "" || wareCount == null || wareCount == "") {
						layer.alert("请输入需要增加的商品或填写数量", {
							title : '提示框',
							icon : 1,
						});

					} else {
						//表格的总行数
						var rows = $("#cureTab tr").length;
						//查询药品名称地址
						var url = "fdprofrom/selectMes.action?wareId="+ wareName;
							$.post(url,null,function(re) {
							   //商品价格
								var wareMoney = re.wareMoney;
								//计量单位
								var measure = re.measure;
								//id
								var wid = re.wareId;
								//药品名称
								var wareName = re.wareName;
								//商品总价
								var wareSum = wareCount* wareMoney;
								//商品条形码
								var warebarco = re.wareFactory;
							//alert("==========="+wareSum+wid+measure+wareMoney+warebarco);
							//叠加同种商品的数量和金额,如果在明细表中可以找到用户刚刚选择增加的商品，那么不重新增加一行，而是叠加到一行中
							if (str.indexOf(wareName) > -1) {
								//叠加数量，wcount
								var counts = parseInt($("#"+ wid+ "_count").html());
								//金额
								var summoneys = parseInt($("#"+ wid+ "_sum").html());
								//得到叠加的数量和金额
								var onecount = counts+ parseInt(wareCount);
								var onemoney = summoneys+ parseInt(wareSum);
								//赋值
								$("#" + wid + "_count").html(onecount+ "/"+ measure);
								$("#" + wid + "_sum").html(onemoney);
							} else {
								//增加一行
								var newRow = "<tr class='text-c'><td style='display:none'>"+wid+"</td><td>"+ warebarco+ "</td><td name='waName'>"+ wareName+ "</td><td>"+ wareMoney+ "</td><td name='wcount' id='"+wid+"_count'>"+ wareCount+ "/"+ measure+ "</td><td name='wsum'id='"+wid+"_sum'>"+ wareSum+ "</td><td class='delete_css' onclick='del(this);'><i class='layui-icon'>&#xe640;</i></td></tr>";
								$("#cureTab tr:last").after(newRow);
							}
							
							
							var string="";
							var dsName = document.getElementsByName("dosName");
							
							for (i = 0; i < dsName.length; i++) {
								string += dsName[i].innerHTML;
							}
							var rows = $("#cure tr").length;
							var url = "dProdesign/plshow.action?wareId="+$("#boards").val()+"&num="+wareCount;
							$.post(url,null,function(map) {
								
									var dosList=map.dosList;
									for(i=0;i<dosList.length;i++){
										//判断是否存在的状态
										var isOn=false;
										//配料编号
										var dosId=dosList[i].dosId;
										//配料的名称
										var dosName= map.dosList[i].dosName;
										//现库存
										var nowNum= map.dosList[i].nowNum;
										//单价
										var price= map.dosList[i].price;
										//配料数量
										var wantCount=map.dosList[i].wantCount;
										//总价
										var cont=map.dosList[i].money;
										//原库存
										var ordNum=map.dosList[i].ordNum;
										//判断库存状态
										var state=map.dosList[i].state;
										var sta="";
										if(state==1){
											sta="<span class='layui-badge layui-bg-gray'>库存充足</span>";
											
										}else{
											sta="<span class='layui-badge layui-bg-orange'>库存不足</span>";
										}
										$("#cure tr").each(function(){
											if($(this).attr("class")==dosId){
												isOn=true;
												
												$(this).find(".wantCount").text(parseFloat($(this).find(".wantCount").text())+parseFloat(wantCount))
												$(this).find(".nowNum").text(parseFloat($(this).find(".nowNum").text())+(parseFloat(nowNum)-parseFloat(ordNum)))
												if($(this).find(".nowNum").text()>=0){
													sta="<span class='layui-badge layui-bg-gray'>库存充足</span>";
												}else{
													sta="<span class='layui-badge layui-bg-orange'>库存不足</span>";
												}
												$(this).find(".state").html(sta)
												$(this).find(".cont").text(parseFloat($(this).find(".cont").text())+parseFloat(cont))
											}
										})
										if(!isOn){
											$("#cure").append("<tr class='"+dosId+"'><td class='dosName'>"+dosName+"</td><td class='price'>"+price+"</td><td class='wantCount'>"+wantCount+"</td><td class='ordNum'>"+ordNum+"</td> <td class='nowNum'>"+nowNum+"</td><td class='cont'>"+cont+"</td><td class='state'>"+sta+"</td></tr>")
										}
									}
								/* for(i=0;i<map.dosList.length;i++){
									//名称
									var dosName= map.dosList[i].dosName;
									//现库存
									var sum= map.dosList[i].nowNum;
									//单价
									var price= map.dosList[i].price;
									//配料数量
									var wantCount=map.dosList[i].wantCount;
									//总价
									var cont=map.dosList[i].money;
									//原库存
									var kucun=map.dosList[i].ordNum;
									
									//现库存的算法
									var xian=kucun-wantCount;
									
									//判断库存状态
									var state=map.dosList[i].state;
									var sta="";
									if(state==1){
										sta="<span class='layui-badge layui-bg-gray'>库存充足</span>";
										
									}else{
										sta="<span class='layui-badge layui-bg-orange'>库存不足</span>";
									}
									
									if(string.indexOf(dosName) > -1){
										//总价格
										var summoney= parseInt($("#"+dosName+ "_money").html());
										//现库存，count
										var count= parseInt($("#"+dosName+ "_nowNum").html());
										//得到叠加的数量和金额
										var onemoney = summoney+parseInt(cont);
										var onecount = xian-parseInt(wantCount);
										//赋值
										$("#" + dosName + "_money").html(onemoney);
										$("#" + dosName + "_nowNum").html(onecount);
									}else{
										var newro="<tr class='text-c'><td name='dosName'>"+dosName+ "</td><td>"+price+ "</td><td name='wantCount'>"+wantCount+ "/"+ measure+ "</td><td name='ordNum'>"+kucun+ "</td><td name='nowNum' id='"+dosName+"_nowNum'>"+xian+ "</td><td name='money' id='"+dosName+"_money'>"+cont+ "</td><td name='state'>"+sta+ "</td></tr>";
										$("#cure").append(newro);
									}
									
								} */
							});
						});
						//清空头部两个文本框
						
						$("#wareCount").val("");
						getNumber();
					}
				});
			});
		
		//增加表单删除一行
		function del(obj) {
			var tr = obj.parentNode;
			var row = tr.rowIndex;
			$("#cureTab tr:eq(" + row + ")").remove();
			getNumber();
		}

		//取数据加到另一个表
		function getNumber() {
			var count = document.getElementsByName("wcount");//数量
			var summoney = document.getElementsByName("wsum");//总价
			var num = 0;
			for (i = 0; i < count.length; i++) {
				//强转为数字
				num += parseInt(count[i].innerHTML);
			}
			var money = 0;
			for (j = 0; j < summoney.length; j++) {
				//强转为数字
				money += parseInt(summoney[j].innerHTML);
			}
			$("#sumMoney").val(money);
			$("#sumNum").val(num);
		}
		
		
		layer.iframeAuto(one);//弹出层自适应
		
	</script>
<script type="text/html" id="titleTpl">
  			{{# if(d.designIsvo==0){ }}
				<span class="layui-badge layui-bg-gray">未审核</span>
			{{#	}else if(d.designIsvo==1){ }}
				<span class="layui-badge layui-bg-orange">等待总经理审核</span>
			{{# }else if(d.designIsvo==2){ }}
				<span class="layui-badge layui-bg-blue">通过审核</span>
			{{# }else if(d.designIsvo==3){ }}
				<span class="layui-badge">打回</span>
			{{# } }}

</script>
</body>

</html>